package com.supermap.dataservice.cloud.api.menu;


import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.api.SysApiMenu;
import com.supermap.dubbo.api.cloud.api.SysApiMenuParams;
import com.supermap.dubbo.api.cloud.cust.role.TreeNodeList;
import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年05月13 11:12
 * @email 1301584033@qq.com
 */
@Mapper
public interface ApiMenuMapper {
    //敏感词过滤
    @SelectProvider(type = ApiMenuMapperSQL.class,method = "getSensiListSQL")
    List<Map<String,Object>> getSensiList(@Param("type") String type,@Param("word")String word,@Param("limit")Integer limit);

    //组
    @Select("select * from alert_info_sensitive_word_library group by SENSITIVETYPE")
    List<Map<String,Object>> getSensiGroupList();
    //添加
    @Insert("insert into alert_info_sensitive_word_library(SENSITIVETYPE,SENSITIVEWORDS) values(#{type},#{word})")
    void  putSensi(@Param("type") String type,@Param("word")String word);
    //删除
    @Delete("delete from alert_info_sensitive_word_library where id=#{id}")
    void  delSensi(@Param("id") Integer id);
    //获取单个接口
    @Select("select * from sys_api_menu where id=#{menuId}")
    Map<String,Object> getApiMenuById(@Param("menuId")Integer menuId);
    //获取参数
    @Select("select * from sys_api_menu_params where apimenuId=#{menuId}")
    List<Map<String,Object>> getMenuByIdParam(@Param("menuId")Integer menuId);
    //获取所有菜单
    @SelectProvider(type = ApiMenuMapperSQL.class,method = "getApiMenuListSQL")
    List<TreeNodeList> getApiMenuList(@Param("pid") Integer pid,@Param("apiUserName") String apiUserName);

    //查出所有用户
    @Select("select id,name from  sys_api_user")
    List<TreeNodeList> getApiAllUserList();


    //增加接口
    //1.增加本身
    @Insert("insert into sys_api_menu(name,url,httpType,pid,returnJosn,remark,catalogue) values(#{sysApiMenu.name},#{sysApiMenu.url},#{sysApiMenu.httpType},#{sysApiMenu.pid},#{sysApiMenu.returnJosn},#{sysApiMenu.remark},#{sysApiMenu.catalogue})")
   // @InsertProvider(type = ApiMenuMapperSQL.class,method = "putApiMenuSQL")
    Integer putApiMenu(@Param("sysApiMenu") SysApiMenu sysApiMenu);
    //2.增加参数
    @Insert("insert into sys_api_menu_params(valueName,name,value,valueType,remark,apimenuId) values(#{sysApiMenuParams.valueName},#{sysApiMenuParams.name},#{sysApiMenuParams.value},#{sysApiMenuParams.valueType},#{sysApiMenuParams.remark},#{sysApiMenuParams.apimenuId})")
   // @InsertProvider(type = ApiMenuMapperSQL.class,method = "putApiMenuParmsSQL")
    Integer putApiMenuParms(@Param("sysApiMenuParams")SysApiMenuParams sysApiMenuParams);
    //3.修改参数
   // @Update("update sys_api_menu_params set valueName=#{sysApiMenuParams.valueName},name=#{sysApiMenuParams.name},value=#{sysApiMenuParams.value},valueType=#{sysApiMenuParams.valueType},remark=#{sysApiMenuParams.remark} where id=#{sysApiMenuParams.id}")
    @UpdateProvider(type = ApiMenuMapperSQL.class,method = "saveApiMenuParmsSQL")
    Integer saveApiMenuParms(@Param("sysApiMenuParams")SysApiMenuParams sysApiMenuParams);
    //4.删除参数
    @Delete("delete from sys_api_menu_params where id=#{id}")
    Integer delApiMenuParms(@Param("id")Integer id);
    //删除接口 逻辑删除
    @Delete("update sys_api_menu set del_flg=1 where id=#{menuId}")
    Integer delApiMenu(@Param("menuId") Integer menuId);
    //修改接口
   // @Update("update sys_api_menu set name=#{sysApiMenu.name},url=#{sysApiMenu.url},httpType=#{sysApiMenu.httpType},returnJosn=#{sysApiMenu.returnJosn},remark=#{sysApiMenu.remark} where id=#{sysApiMenu.id}")
    @UpdateProvider(type = ApiMenuMapperSQL.class,method = "saveApiMenuSQL")
    Integer saveApiMenu(@Param("sysApiMenu") SysApiMenu sysApiMenu);

    class ApiMenuMapperSQL{
        public String getSensiListSQL(Map<String,Object> map){
            StringBuilder stringBuilder=new StringBuilder();
            String type=(String) map.get("type");
            String word=(String) map.get("word");
            Integer limit=(Integer) map.get("limit");
            stringBuilder.append(" select * from alert_info_sensitive_word_library where 1=1");
            if(Strings.isNotBlank(type)){
                stringBuilder.append(" and SENSITIVETYPE='"+type+"' ");
            }
            if(Strings.isNotBlank(word)){
                stringBuilder.append(" and  SENSITIVEWORDS like concat('%','"+word+"','%') ");
            }
            stringBuilder.append(" order by ctime desc limit "+limit+"");
            return  stringBuilder.toString();
        }
        public String saveApiMenuParmsSQL(Map<String,Object> map){
            SysApiMenuParams sysApiMenuParams=(SysApiMenuParams)map.get("sysApiMenuParams");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" update sys_api_menu_params set id="+sysApiMenuParams.getId()+" ");
            if(Strings.isNotBlank(sysApiMenuParams.getValueName())){
                stringBuilder.append(" ,valueName='"+sysApiMenuParams.getValueName()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenuParams.getName())){
                stringBuilder.append(" ,name='"+sysApiMenuParams.getName()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenuParams.getValue())){
                stringBuilder.append(" ,value='"+sysApiMenuParams.getValue()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenuParams.getValueType())){
                stringBuilder.append(" ,valueType='"+sysApiMenuParams.getValueType()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenuParams.getRemark())){
                stringBuilder.append(" ,remark='"+sysApiMenuParams.getRemark()+"' ");
            }
            stringBuilder.append(" where id="+sysApiMenuParams.getId()+" ");
            return  stringBuilder.toString();
        }
        public String saveApiMenuSQL(Map<String,Object> map){
            SysApiMenu sysApiMenu=(SysApiMenu)map.get("sysApiMenu");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(" update sys_api_menu set id="+sysApiMenu.getId()+" ");
            if(Strings.isNotBlank(sysApiMenu.getName())){
                stringBuilder.append(" ,name='"+sysApiMenu.getName()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenu.getUrl())){
                stringBuilder.append(" ,url='"+sysApiMenu.getUrl()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenu.getHttpType())){
                stringBuilder.append(" ,httpType='"+sysApiMenu.getHttpType()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenu.getReturnJosn())){
                stringBuilder.append(" ,returnJosn='"+sysApiMenu.getReturnJosn()+"' ");
            }
            if(Strings.isNotBlank(sysApiMenu.getRemark())){
                stringBuilder.append(" ,remark='"+sysApiMenu.getRemark()+"' ");
            }
            stringBuilder.append(" where id="+sysApiMenu.getId()+" ");
            return  stringBuilder.toString();
        }

        public String getApiMenuListSQL(Map<String,Object> map){
            Integer pid=(Integer) map.get("pid");
            String apiUserName=(String) map.get("apiUserName");
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append("select id,name,catalogue as category,del_flg as state,pid from sys_api_menu where  del_flg=0 ");
            if(pid!=null){
                stringBuilder.append(" and pid="+pid+" ");
            }
            if(apiUserName!=null){
                stringBuilder.append(" and id in(select apimenuId from sys_api_menu_user t1 inner join sys_api_user t2 on t1.apiuserId=t2.id and t2.del_flg=0 and  t1.del_flg=0 and t2.name='"+apiUserName+"' )");
            }
            return  stringBuilder.toString();
        }


    }


}
